﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel.OutPut;
using Model.BusinessModel;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System.Text;
using Model;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 按钮仓储
    /// </summary>
    public class SysButtonRepository : Repository<SysButton>, ISysButtonRepository
    {

        /// <summary>
        /// 根据菜单id获取按钮
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <returns>返回对应按钮集合</returns>
        public List<SysButtonDataOutPut> GetButtonByMenuId(string menuId)
        {
            string sql = @" select *  from  Sys_Button  where MenuId = @MenuId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysButtonDataOutPut>(sql, new { MenuId = menuId }).ToList();
        }

        /// <summary>
        /// 根据菜单id获取按钮及样式
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <returns>返回对应按钮集合</returns>
        public List<SysButtonDataOutPut> GetButtonStyleByMenuId(string menuId)
        {
            string sql = @" select sd.AnalysisRule,sd.IsOpen RuleIsOpen,sb.ButtonId,sb.ButtonRoleType,sb.RuleId,sb.MenuId,sb.ButtonName,sb.CorporationKey,sb.SystemKey,sb.ButtonKey,bs.ButtonStyleName,bs.Icon,bs.Color,bs.TextColor,bs.Types,bs.Size,bs.IsText,bs.IsRound,bs.IsCircle,bs.IsPlain,bs.IsLink,bs.IsDisabled,sb.OrderBy from Sys_Button as sb 
              inner join Sys_ButtonStyle as bs on sb.ButtonStyleId = bs.ButtonStyleId
              left join  Sys_DataRule as sd on sd.RuleId = sb.RuleId and sb.MenuId = sd.MenuId
  where sb.MenuId = @MenuId ORDER BY OrderBy  ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysButtonDataOutPut>(sql, new { MenuId = menuId }).ToList();
        }

        /// <summary>
        /// 根据按钮id获取数据
        /// </summary>
        /// <param name="buttonIds">按钮id集合</param>
        /// <returns>返回对应按钮集合</returns>
        public List<SysButton> GetButtonByButtonId(List<string> buttonIds)
        {
            string sql = @" select *  from  Sys_Button  where ButtonId in @ButtonId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysButton>(sql, new { ButtonId = buttonIds.ToArray() }).ToList();
        }

        /// <summary>
        /// 获取公司系统按钮数据
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <param name="buttonRoleType">按钮类型</param>
        /// <returns></returns>
        public List<SysButtonDataOutPut> GetCorporationSystemButtonList(string corporationKey, string systemKey,int buttonRoleType)
        {
            StringBuilder sql = new(@" select sb.ButtonId,sb.ButtonRoleType,sb.MenuId,sb.ButtonName,sb.CorporationKey,sb.SystemKey,sb.ButtonKey,bs.ButtonStyleName,bs.Icon,bs.Color,bs.TextColor,bs.Types,bs.Size,bs.IsText,bs.IsRound,bs.IsCircle,bs.IsPlain,bs.IsLink,bs.IsDisabled from Sys_Button as sb 
              inner join Sys_ButtonStyle as bs on sb.ButtonStyleId = bs.ButtonStyleId
              inner join Sys_Menu as sm on sb.MenuId = sm.Id ");

            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(corporationKey) && Const.XC_CorporationKey != corporationKey)
            {
                qp.listWhere.Append(" and sm.CorporationKey = @CorporationKey ");
                qp.dynamicParameter.Add("@CorporationKey", corporationKey);
            }
            if (!string.IsNullOrWhiteSpace(systemKey))
            {
                qp.listWhere.Append(" and sm.SystemKey = @SystemKey ");
                qp.dynamicParameter.Add("@SystemKey", systemKey);
            }
            if (buttonRoleType !=0)
            {
                qp.listWhere.Append(" and sb.ButtonRoleType = @ButtonRoleType ");
                qp.dynamicParameter.Add("@ButtonRoleType", buttonRoleType);
            }

            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysButtonDataOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 更新按钮绑定数据规则
        /// </summary>
        /// <param name="buttonIds">按钮id集合</param>
        /// <param name="ruleId">数据规则id</param>
        public void UpdataRuleIdByButtonId(List<string> buttonIds,string ruleId ) 
        {
            string sql = " update Sys_Button set RuleId = @RuleId  where ButtonId in @ButtonId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { ButtonId = buttonIds, RuleId= ruleId });
        }

        /// <summary>
        /// 根据规则id,清空按钮规则
        /// </summary>
        /// <param name="ruleId">数据规则id</param>
        public void ClearButtonRule(string ruleId)
        {
            string sql = " update Sys_Button set RuleId = ''  where RuleId = @RuleId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { RuleId = ruleId });
        }
    }
}
